﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ContactManager.Models.Entities;
using StructureMap;
using ContactManager.Models.Validation;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using ContactManager.Models;

namespace ContactManager
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
             
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
               "AllContacts",                               // Route name
               "Contacts/Page/{page}",                           // URL with params
               new { controller = "Home", action = "Index" } // Param defaults
           );

            routes.MapRoute(
                           "AllGroups",                               // Route name
                           "Groups/Page/{page}",                           // URL with params
                           new { controller = "Group", action = "Index" } // Param defaults
                       );
            routes.MapRoute(
                "Home", // Route name
                "Home", // URL with parameters
                new { controller = "Contact", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "HomeWithGroup", // Route name
                "Home/{id}", // URL with parameters
                new { controller = "Contact", action = "Index"} // Parameter defaults
            );
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Contact", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();


            RegisterRoutes(RouteTable.Routes);



            IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activeRecord") as Castle.ActiveRecord.Framework.IConfigurationSource;

            //Assembly asm1 = typeof(ONE OF YOUR ASSEMBLIES, I.E. CONTACT).Assembly;

            //ActiveRecordStarter.Initialize(new Assembly[] { asm1 }, source);
            ActiveRecordStarter.Initialize(source, typeof(ContactGroup),typeof(Contact));



            //log4net.Config.XmlConfigurator.Configure();


            //Set Up Object Factory with 1 or + Repository Registries
            //A individual RR maps interface(service provider) with service implementator
            Bootstrapper.Bootstrap();

            //Do it


           // StructureMapConfiguration.Configure();

            
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());


            /*

            ObjectFactory.Initialize(registry => 
                {
                    registry
        .For<IContactManagerRepository>().Use<ContactManagerRepository>();
                    registry
         .For<IContactManagerService>()  .Use<ContactManagerService>();

                    registry
                    .For<IModelStateWrapper>().Use<ModelStateWrapper>();            
                });

           */

            

        }
    }
}